package com.qf.filter;



import com.qf.Entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*2021/10/9*/
@WebFilter("/index.jsp")
public class login implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request=(HttpServletRequest)req;
        HttpServletResponse response=(HttpServletResponse)resp;
        User user = (User)request.getSession().getAttribute("user");
        //判断用户是否登录，如果已经登录，则不能再次进入登录页面
        if(user!=null){
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }else{
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
